package com.kmust.dream.wanderingdream.shoppingcart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmust.dream.wanderingdream.comm.pojo.ResultBean;
import com.kmust.dream.wanderingdream.shoppingcart.entity.ShoppingCartItem;
import com.kmust.dream.wanderingdream.shoppingcart.service.ShoppingCartItemService;
import com.kmust.dream.wanderingdream.shoppingcart.mapper.ShoppingCartItemMapper;
import com.kmust.dream.wanderingdream.shoppingcart.utils.ReturnUtils;
import com.kmust.dream.wanderingdream.shoppingcart.vo.SciOrderPreVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class ShoppingCartItemServiceImpl extends ServiceImpl<ShoppingCartItemMapper, ShoppingCartItem>
        implements ShoppingCartItemService {

    @Override
    public ResultBean getItemsByUsername(String username) {
        List<ShoppingCartItem> shoppingCartItems = list(new QueryWrapper<ShoppingCartItem>().eq("username", username).eq("show",true));
        return ReturnUtils.getList(shoppingCartItems);
    }

    @Override
    public ResultBean addItem(ShoppingCartItem shoppingCartItem) {
        return ReturnUtils.add(save(shoppingCartItem));
    }

    @Override
    public ResultBean updateItem(ShoppingCartItem shoppingCartItem) {
        return ReturnUtils.update(updateById(shoppingCartItem));
    }

    @Override
    public ResultBean deleteItem(Integer itemId) {
        return ReturnUtils.delete(removeById(itemId));
    }

    @Override
    public ResultBean updateItemNumber(Integer itemId, Integer flagNumber, Integer stock) {
        if (flagNumber > 0 && flagNumber <= stock)
            return ReturnUtils.update(update(new UpdateWrapper<ShoppingCartItem>()
                    .eq("item_id", itemId)
                    .set("item_number", flagNumber)));
        return ReturnUtils.update(false);
    }

    @Override
    public ResultBean getSciDel(String username) {
        QueryWrapper<ShoppingCartItem> queryWrapper = new QueryWrapper<ShoppingCartItem>().eq("username", username).eq("show", false);
        return ReturnUtils.getList(list(queryWrapper));
    }

    @Override
    public ResultBean createOrder(SciOrderPreVO sciOrderPreVO) {
        return null;
    }


    @Override
    public ResultBean getAllItems() {
        return ReturnUtils.getList(list());
    }
}